package cn.ruanyun.backInterface.modules.business.order.TimingTask;

import cn.hutool.core.util.ObjectUtil;
import cn.ruanyun.backInterface.common.enums.BuyTypeEnum;
import cn.ruanyun.backInterface.common.enums.GoodTypeEnum;
import cn.ruanyun.backInterface.common.enums.OrderStatusEnum;
import cn.ruanyun.backInterface.common.enums.OrderTypeEnum;
import cn.ruanyun.backInterface.modules.business.comment.pojo.Comment;
import cn.ruanyun.backInterface.modules.business.comment.service.ICommentService;
import cn.ruanyun.backInterface.modules.business.commonParam.service.IcommonParamService;
import cn.ruanyun.backInterface.modules.business.order.pojo.Order;
import cn.ruanyun.backInterface.modules.business.order.service.IOrderService;
import cn.ruanyun.backInterface.modules.business.orderDetail.service.IOrderDetailService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Calendar;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@Configuration
@EnableScheduling
@Slf4j
public class OrderTimingTask {


    @Autowired
    private IOrderService orderService;

    @Autowired
    private IcommonParamService icommonParamService;

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IOrderDetailService orderDetailService;


    //自动确认收获
    @Scheduled(cron = "0 0 0 * * ?")
    public void resolveAutoReceiveOrder() {

        Integer autoReceiveOrderTime = icommonParamService.getCommonParamVo().getAutoReceiveOrderTime();

        Optional.ofNullable(orderService.list(Wrappers.<Order>lambdaQuery().eq(Order::getOrderStatus, OrderStatusEnum.DELIVER_SEND)))
                .ifPresent(orders -> orders.parallelStream().forEach(order -> {

                    //1.筛选出来订单时间大于或者等于确认收获时间的订单
                    if ((getDay(new Date()) - getDay(order.getCreateTime()) > autoReceiveOrderTime) || (getDay(new Date()) - getDay(order.getCreateTime()) == autoReceiveOrderTime)) {

                        if (ObjectUtil.equal(BuyTypeEnum.FULL_PURCHASE, order.getBuyType())) {

                            order.setOrderStatus(OrderStatusEnum.PRE_COMMENT);

                        }else {

                            order.setOrderStatus(OrderStatusEnum.SETTLE_ACCOUNTS);
                        }

                        orderService.updateById(order);
                    }
                }));
    }


    //自动评价
    @Scheduled(cron = "0 0 0 * * ?")
    public void resolveAutoCommentOrder() {

        Integer autoCommentOrderTime = icommonParamService.getCommonParamVo().getAutoCommentOrderTime();

        Optional.ofNullable(orderService.list(Wrappers.<Order>lambdaQuery().eq(Order::getOrderStatus, OrderStatusEnum.PRE_COMMENT)))
                .ifPresent(orders -> orders.parallelStream().forEach(order -> {

                    //1.筛选出来订单时间大于或者小于自动好评订单时间
                    if ((getDay(new Date()) - getDay(order.getCreateTime()) > autoCommentOrderTime) || (getDay(new Date()) - getDay(order.getCreateTime()) == autoCommentOrderTime)) {


                        //异步插入评论
                        CompletableFuture.runAsync(() -> {

                            Comment comment = new Comment();
                            comment.setOrderId(order.getId());
                            if (ObjectUtil.equal(OrderTypeEnum.GOOD, order.getTypeEnum())) {

                                comment.setTypeEnum(GoodTypeEnum.GOOD);
                            }else if (ObjectUtil.equal(OrderTypeEnum.GOODS_PACKAGE, order.getTypeEnum())) {

                                comment.setTypeEnum(GoodTypeEnum.GOODSPACKAGE);
                            }
                            comment.setUserId(order.getUserId());
                            comment.setContent("默认好评");
                            comment.setStartLevel(5.0);

                            //商品id
                            comment.setGoodId(orderDetailService.getOrderDetailByOrderId(order.getId()).getGoodId());
                            commentService.save(comment);
                        }).join();

                        //更新订单
                        order.setOrderStatus(OrderStatusEnum.IS_COMPLETE);
                        orderService.updateById(order);
                    }

                }));

    }


    //自动评价
    @Scheduled(cron = "0 0 0 * * ?")
    public void resolveAutoConfirmReturn() {

        Integer autoConfirmReturnMoneyTime = icommonParamService.getCommonParamVo().getAutoConfirmReturnMoneyTime();

        Optional.ofNullable(orderService.list(Wrappers.<Order>lambdaQuery().eq(Order::getOrderStatus, OrderStatusEnum.PRE_CONFIRM_RETURN)))
                .ifPresent(orders -> orders.parallelStream().forEach(order -> {

                    //1.筛选出来订单时间大于或者小于自动好评订单时间
                    if ((getDay(new Date()) - getDay(order.getCreateTime()) > autoConfirmReturnMoneyTime) || (getDay(new Date()) - getDay(order.getCreateTime()) == autoConfirmReturnMoneyTime)) {

                        //更新订单
                        order.setOrderStatus(OrderStatusEnum.RETURN_FINISH);
                        orderService.updateById(order);
                    }

                }));

    }

    //自动取消订单
    @Scheduled(cron = " 0 20 1 ? * *")
    public void resolveAutoConfirmCancelOrder() {

        Integer cancelOrder = icommonParamService.getCommonParamVo().getCancelOrder();

        Optional.ofNullable(orderService.list(Wrappers.<Order>lambdaQuery().eq(Order::getOrderStatus, OrderStatusEnum.PRE_PAY)))
                .ifPresent(orders -> orders.parallelStream().forEach(order -> {

                    //1.筛选出来订单时间大于或者小于自动好评订单时间
                    if ((getDay(new Date()) - getDay(order.getCreateTime()) > cancelOrder) || (getDay(new Date()) - getDay(order.getCreateTime()) == cancelOrder)) {

                        //更新订单
                        order.setOrderStatus(OrderStatusEnum.CANCEL_ORDER);
                        orderService.updateById(order);
                    }
                }));

    }



    /**
     * 獲取当前天
     * @param date  date
     * @return Integer
     */
    public Integer getDay(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

}
